using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrMaritalStatus : System.Web.UI.UserControl
{
    protected string strMaritalStatus = "-1";
    protected string strValidatinoGroup = "";
    protected string strErrorMessage = "Choose marital status";
    protected bool bEnabled = true;
    bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dtMaritalStatus = General.GetComboItem("TH.MaritalStatus", true);
            ddlMaritalStatus.DataSource = dtMaritalStatus;
            ddlMaritalStatus.DataTextField = dtMaritalStatus.Columns[0].ColumnName.ToString();
            ddlMaritalStatus.DataValueField = dtMaritalStatus.Columns[1].ColumnName.ToString();
            ddlMaritalStatus.DataBind();
            ddlMaritalStatus.Items.Insert(0, new ListItem("Select", "-1"));

            ddlMaritalStatus.Attributes.Add("onblur", "ShowValidationEffect('" + ddlMaritalStatus.ClientID + "','" + rfvMaritalStatus.ClientID + "');");
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvMaritalStatus.Enabled && rfvMaritalStatus.ValidationGroup == strValidationGroup)
        {
            rfvMaritalStatus.Validate();
            if (rfvMaritalStatus.IsValid == false)
            {
                ddlMaritalStatus.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlMaritalStatus.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlMaritalStatus.ControlStyle.CssClass = "";
        }
    }
    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the value is -1
    //        if (args.Value != "-1")
    //        {
    //            args.IsValid = true;
    //        }
    //        else
    //        {
    //            args.IsValid = false;
    //        }
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvMaritalStatus.Enabled = bValue;
    }

    public string MaritalStatus
    {
        get { return strMaritalStatus; }
        set { strMaritalStatus = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlMaritalStatus.SelectedIndex = 0;
        ddlMaritalStatus.Enabled = bEnabled;
        rfvMaritalStatus.Enabled = bEnabled;
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        if (strMaritalStatus != string.Empty)
        {
            if (strMaritalStatus != "-1")
                ddlMaritalStatus.Text = strMaritalStatus;
        }

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
                rfvMaritalStatus.ValidationGroup = strValidatinoGroup;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strErrorMessage != string.Empty)
        {
            //rfvMaritalStatus.ErrorMessage = strErrorMessage;
        }
    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlMaritalStatus.Text = strMaritalStatus;

            string orgMaritalStatusText = ddlMaritalStatus.SelectedItem.Text;
            string orgMaritalStatusValue = ddlMaritalStatus.SelectedItem.Value;

            ddlMaritalStatus.Items.Clear();

            ddlMaritalStatus.Items.Add(new ListItem(orgMaritalStatusText, orgMaritalStatusValue));
        }
    }

    public string GetMaritalStatus()
    {
        strMaritalStatus = ddlMaritalStatus.SelectedValue;
        return strMaritalStatus;
    }
}
